package controller;

import java.io.*;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import com.advm.bean.ADInfo;
import com.advm.bean.PageBean;
import com.advm.dao.VideoDAO;

import net.sf.json.JSONArray;

@Controller
public class CommonController {
	
	public  VideoDAO dao = null;
	public List<ADInfo> listInfo = null;
	@RequestMapping(value="/")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response)
	{ 
		return new ModelAndView("login_info");
	}
	@RequestMapping(value = "index.do")
	public void index_jsp(Model model)
	{
	//	System.out.println("index.jsp");
	}
	@RequestMapping(value = "upload_index.do")
	public void upload_index()
	{
	//	System.out.println("upload_index.do");
	}
	@RequestMapping(value="upload")
	public String upload(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException, SQLException 
	{
		PrintWriter out = response.getWriter();
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getServletContext());
		if(multipartResolver.isMultipart(request))
		{
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			Iterator<String> iter = multiRequest.getFileNames();
			while(iter.hasNext())
			{
				int startTime = (int)System.currentTimeMillis();
				MultipartFile file = multiRequest.getFile(iter.next());
				String fileName = null;
				String path = null;
				if(file != null)
				{
					String myFilename = file.getOriginalFilename();
					if(myFilename.trim() != "")
					{
						fileName = "demo_" + file.getOriginalFilename();
						path = "E:/" + fileName;
						File localFile = new File(path);
						file.transferTo(localFile);		
					}
				}
				
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
				String lastTime = df.format(new Date());// 
				String lable = "nba";
				System.out.println(lastTime);
				ADInfo ad = new ADInfo(fileName, path,lable,lastTime);
				dao = new VideoDAO();
				dao.add(ad);
			}
		}
//		out.println("upload success");
//		out.flush();
		return "upload_index";
	}
	@RequestMapping("download")
	public String download(String fileName,HttpServletResponse response) throws IOException, SQLException
	{
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName="+ fileName);
		//String path = Thread.currentThread().getContextClassLoader().getResource("E://").getPath();
		dao = new VideoDAO();
		String querryPath = dao.querry(fileName);
		if(querryPath != null)
		{
			InputStream is = new FileInputStream(new File(querryPath));
			OutputStream os = response.getOutputStream();
			byte[] bytes = new byte[2048];
			int len = 0;
			while((len = is.read(bytes)) > 0)
			{
				os.write(bytes, 0, len);
			}
			os.close();
			is.close();
			return "download_success";
		}	
		return null;
	}
	@RequestMapping("delete")
	public String delete( int id,HttpServletResponse response) throws SQLException, IOException
	{
		PrintWriter out = response.getWriter();
		dao = new VideoDAO();
		int msg =dao.delete(id);
		if(msg == 1)
			return "adinfo";
		else 
		{
			out.println("delete Error!");
			out.flush();
			return "adinfo";
		}
		
	}
	
	
	@RequestMapping("adinfo")
	public void adinfo() 
	{		
//		@ModelAttribute("info") Form form
//		dao = new VideoDAO();
//		if(listInfo == null || listInfo.isEmpty())
//		{
//			listInfo = new ArrayList<ADInfo>();
//			listInfo = dao.querryAll();
//		}
//		 model.addAttribute("info", listInfo);
//		return listInfo;
	/*	int curPage;
		if(pageBean.getCurPage() == 0)
		{
			curPage = 1;
		}else
		{
			curPage = pageBean.getCurPage();
		}
		
		
		PageBean page1 = new PageBean();
		PageBean page2 = page1.getResult(curPage);
		// 把PageBean保存到request对象中。注意：viewpage.jsp里jsp:useBean
		// id必须为才可以"page2"！！！
//		request.getSession().setAttribute("info", page2.getResult(page).data);
//		request.setAttribute("page2",page2);
//		request.getRequestDispatcher("adinfo.jsp").forward(request, response);
		model.addAttribute("info", page2.getResult(curPage).data);
		model.addAttribute("page2", curPage);
		*/
	}
	@RequestMapping("login_info")
	public void login_info()
	{
		
	}
	@RequestMapping("login")
	public void login(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		if(userName.equals("admin") && pwd.equals("123456"))
		{
			out.println("success");
		}else
		{
			out.print("false");
		}
		out.close();
	}

}
